import { reactive } from 'vue';
import { AssetDto } from './index';
import { ItemTypeDto } from '../../item-receive/edit-single/index';
const state: {
  /**
   * 资产类型列表
   */
  assetList: AssetDto[];
  //标题索引
  titleAssetIndex: number;
  /**
   *耗材类型列表
   */
  spareList: ItemTypeDto[];
  //标题索引
  titleSpareIndex: number;
  /**
   * 审批流程配置图结构体
   */
  flowGraph?: FlowPathTemplateApi.FlowGraph;
  /**
   * 审批流程配置图结构体
   */
  flowGraphAddFlow?: FlowPathTemplateApi.FlowGraph;
  /**
   *审批记录表单属性信息
   */
  attributes?: FlowPathTemplateApi.AttributeDto[];
  /**
   * 报修类型
   */
  repairType?: string;
  /**
   * 报修说明
   */
  desc?: string;
  /**
   * user的uaa
   */
  userId?: string;
  /**
   * templateId
   */
  templateId?: string;
  /**
   * 产品列表
   */
  sourceProductList: IbFpProductApi.Dto[];
  /**
   * 申请预算 (budget)
   */
  budget: string;
  /**
   * 是否存在属性条件分支（1-是，2-否）
   */
  conditionAttr?: 1 | 2;
} = reactive({
  assetList: [],
  titleAssetIndex: 0,
  spareList: [],
  titleSpareIndex: 0,
  flowGraph: {},
  flowGraphAddFlow: {},
  attributes: [],
  repairType: '',
  desc: '',
  templateId: '',
  sourceProductList: [],
  budget: '',
  conditionAttr: 2,
});

/**
 * 删除item
 */
function deleteAssetItem(id: string) {
  const index = state.assetList.findIndex((item) => item.id === id);
  state.assetList.splice(index, 1);
}

/**
 * 添加item
 */
function addAssetItem() {
  state.titleAssetIndex++;
  state.assetList.push({
    id: state.titleAssetIndex.toString(),
    deletable: true,
    title: '报修资产' + state.titleAssetIndex,
  });
}

/**
 * 添加item
 */
function addAssetItemByDraft(item: FlowPathDraftApi.Asset) {
  state.titleAssetIndex++;
  state.assetList.push({
    ...item,
    id: state.titleAssetIndex.toString(),
    deletable: true,
    title: '报修资产' + state.titleAssetIndex,
  });
}

/**
 * 复制资产item
 */
function copyAssetItem(item: AssetDto) {
  state.titleAssetIndex++;
  state.assetList.push({
    ...item,
    id: state.titleAssetIndex.toString(),
    deletable: true,
    title: '报修资产' + state.titleAssetIndex,
  });
}

/**
 * 删除item
 */
function deleteSpareItem(id: string) {
  const index = state.spareList.findIndex((item) => item.id === id);
  state.spareList.splice(index, 1);
}

/**
 * 添加item
 */
function addSpareItem() {
  state.titleSpareIndex++;
  state.spareList.push({
    id: state.titleSpareIndex.toString(),
    deletable: true,
    title: '耗材品类' + state.titleSpareIndex,
  });
}

/**
 * 添加item
 */
function addSpareItemByDraft(spare: FlowPathDraftApi.Product) {
  state.titleSpareIndex++;
  state.spareList.push({
    ...spare,
    id: state.titleSpareIndex.toString(),
    deletable: true,
    title: '耗材品类' + state.titleSpareIndex,
  });
}

/**
 * 耗材
 * @param item 复制耗材项
 */
function copySpareItem(item: ItemTypeDto) {
  state.titleSpareIndex++;
  state.spareList.push({
    ...item,
    id: state.titleSpareIndex.toString(),
    deletable: true,
    title: '耗材品类' + state.titleSpareIndex,
  });
}

/**
 * 清空items的数据
 */
function clearItems() {
  state.assetList = [];
  state.spareList = [];
  state.titleAssetIndex = 0;
  state.titleSpareIndex = 0;
  state.desc = '';
  state.budget = '';
}

export {
  state,
  deleteAssetItem,
  addAssetItem,
  copyAssetItem,
  deleteSpareItem,
  addSpareItem,
  copySpareItem,
  clearItems,
  addAssetItemByDraft,
  addSpareItemByDraft,
};
